<?php

class PollConfiguration extends Configuration {
	protected $AppName = "poll";
	public $UseApps = array(array("name" => "smarty"), array("name" => "mysql"), array("name" => "redirector"));
	protected $IncludeFiles = array("poll.class.php", "poll.item.class.php");
	protected $HeadTags = array("<link rel=\"stylesheet\" type=\"text/css\" href=\"{\$env.url}lib/poll/views/poll.css\" />");

	function registerMethods(AdminController $admin){
		// Public interfaces
		$admin->register($this->AppName, "add-poll", "addPollInterface", $this->Language->get("add-poll"));			// DONE
		$admin->register($this->AppName, "list-polls", "listPollsInterface", $this->Language->get("list-polls"));

		// Private interfaces
		$admin->register($this->AppName, "edit-poll", "editPollInterface", null);									// DONE
		$admin->register($this->AppName, "edit-poll-item", "editPollItemInterface", null);							// DONE

		// THE workers :P
		$admin->register($this->AppName, "update-poll", "updatePoll", null);										// DONE
		$admin->register($this->AppName, "update-poll-item", "updatePollItem", null);								// DONE
		$admin->register($this->AppName, "delete-poll", "deletePoll", null);										// DONE
		$admin->register($this->AppName, "delete-poll-item", "deletePollItem", null);								// DONE
	}

	function listPollsInterface(){
		$MySQL = $this->UseApps["mysql"];

		$PollsQuery = $MySQL->query("SELECT id FROM {$MySQL->getPrefix()}polls ORDER BY started_on DESC");

		if(mysql_num_rows($PollsQuery) <= 0){
			return $this->Language->get("no-polls");
		}

		$Polls = array();

		while($p = mysql_fetch_object($PollsQuery)){
			$Polls[] = new Poll($p->id, $this);
		}

		$SmartyC = $this->UseApps["smarty"];
		$Smarty = $SmartyC->getSmarty();

		$Smarty->assign("PollList", $Polls);

		$this->ViewFile = "config_list_polls.tpl";
		return true;
	}

	function addPollInterface(){
		$SmartyC = $this->UseApps["smarty"];
		$SmartyC->addHeadTag("<script type=\"text/javascript\" src=\"{\$env.url}lib/poll/poll.js\"></script>");

		$this->ViewFile = "config_add_poll.tpl";
		return true;
	}

	function editPollInterface(){
		$URLPosition = SystemRegistry::getInstance()->get("url_position");
		$HTTP = new HTTPRequest();

		$PollID = $HTTP->get($URLPosition+3);

		if(!is_numeric($PollID)){
			return $this->Language->get("poll-id-not-numeric");
		}

		$Poll = new Poll($PollID, $this);

		$SmartyC = $this->UseApps["smarty"];
		$SmartyC->addHeadTag("<script type=\"text/javascript\" src=\"{\$env.url}lib/poll/poll.js\"></script>");
		$Smarty = $SmartyC->getSmarty();

		$Smarty->assign("Poll", $Poll);

		$this->ViewFile = "config_edit_poll.tpl";
		return true;
	}

	function editPollItemInterface(){
		$URLPosition = SystemRegistry::getInstance()->get("url_position");
		$HTTP = new HTTPRequest();

		$AnswerID = $HTTP->get($URLPosition+3);

		if(!is_numeric($AnswerID)){
			return false;
		}

		$Answer = new PollItem($AnswerID, $this);

		$SmartyC = $this->UseApps["smarty"];
		$Smarty = $SmartyC->getSmarty();

		$Smarty->assign("PollItem", $Answer);

		$this->ViewFile = "config_edit_item.tpl";
		return true;
	}

	function updatePoll(){
		$URLPosition = SystemRegistry::getInstance()->get("url_position");
		$HTTP = new HTTPRequest();

		$PollID = $HTTP->get($URLPosition+3);

		if($PollID == false){
			$Poll = new Poll(null, $this);
		}
		else {
			if(!is_numeric($PollID)){
				return false;
			}
			$Poll = new Poll($PollID, $this);
		}

		$MySQL = $this->UseApps["mysql"];

		// prepare data
		$Date = explode(".", $_POST["poll-pick-date"]);
		$Timestamp = mktime(null, null, null, $Date[1], $Date[0], $Date[2]);
		$MembersOnly = ($_POST["poll-members-only"] == "yes") ? true : false;
		$ShowResults = ($_POST["poll-show-results"] == "yes") ? true : false;
		$Active = ($_POST["poll-active"] == "yes") ? true : false;
		$Question = escape($_POST["poll-question"], $MySQL);

		$Poll->setQuestion($Question);
		$Poll->setExpiresOn($Timestamp);
		$Poll->setActive($Active);
		$Poll->setShowResults($ShowResults);
		$Poll->setMembersOnly($MembersOnly);

		if($Poll->update() == true){
			$Redirector = $this->UseApps["redirector"];
			$Redirector->redirect(Core::$FMSConfig->getValue("environment", "url")."admin/configure/poll/edit-poll/".$Poll->getID()."/");
		}

		return $this->Language->get("error-creating-poll");
	}

	function updatePollItem(){
		$URLPosition = SystemRegistry::getInstance()->get("url_position");
		$HTTP = new HTTPRequest();

		$AnswerID = $HTTP->get($URLPosition+3);

		if($AnswerID == false){
			$Answer = new PollItem(null, $this);
			$Answer->setVotes(0);
		}
		else {
			if(!is_numeric($AnswerID)){
				return false;
			}

			$Answer = new PollItem($AnswerID, $this);
			$Answer->setVotes(intval($_POST["answer_votes"]));
		}

		$Answer->setLabel(escape($_POST["answer_label"], $this->UseApps["mysql"]));
		$Answer->setPollID(intval($_POST["poll_id"]));

		$Answer->update();

		$IsAjax = $HTTP->get($URLPosition-2);

		if($IsAjax == false){
			return true;
		}

		return "true";
	}

	function deletePoll(){
		$URLPosition = SystemRegistry::getInstance()->get("url_position");
		$HTTP = new HTTPRequest();

		$PollID = $HTTP->get($URLPosition+3);

		if(!is_numeric($PollID)){
			return false;
		}

		$Poll = new Poll($PollID, $this);

		$PollItems = $Poll->getPollItems();

		foreach($PollItems as $Item){
			$Item->delete();
		}

		$Poll->delete();

		$IsAjax = $HTTP->get($URLPosition-2);

		if($IsAjax == false){
			return $this->Language->get("poll-answers-deleted");
		}

		return "true";
	}

	function deletePollItem(){
		$URLPosition = SystemRegistry::getInstance()->get("url_position");
		$HTTP = new HTTPRequest();

		$ItemID = $HTTP->get($URLPosition + 3);

		if(!is_numeric($ItemID)){
			return false;
		}

		$Item = new PollItem($ItemID, $this);

		$Item->delete();

		$IsAjax = $HTTP->get($URLPosition-2);

		if($IsAjax == false){
			return $this->Language->get("answer-deleted");
		}

		return "true";
	}
}

?>